﻿// Pour faire des tests bajax
// sebastian Mosser

// effectue une invocation introspective

function doInvoke() {
	var className = document.getElementById("class").value;
	var methodName = document.getElementById("method").value;
	var params = document.getElementsByName("param");
	//alert(params);
	if (className.length == 0)
		return manageError("Le nom de la classe est vide ;'( ");
	if (methodName.length == 0)
		return manageError("Le nom de la méthode est vide ;'( ");	
		
	var parameters = new Array();

  	
	for (var i = 0; i < params.length; i++) {
		parameters[i] = params[i].value;
	}
	  
	displayQuery(className,methodName,parameters);
	displayTitle(className,methodName,parameters);

	
	invoke(1,className,methodName,parameters,manageResult);	
		
}


function displayTitle(className, methodName, params) {
  var titleElem = document.getElementById("answerTitle");
  var newTitle = "<h2> <code>" + className +"::bajax_"+methodName+"(";
  newTitle += "[";
  for(var p in params)
    newTitle += "'"+params[p]+"',";
  newTitle = newTitle.substring(0,newTitle.length - 1);
  newTitle += "])</code></h2>";
  titleElem.innerHTML = newTitle;

}

function addParameter() {
  var zeDiv = document.getElementById("parameters");
  var aParam = "<label>";
  aParam += "<span> Paramètre : </span>";
  aParam += "<input type=\"text\" name=\"param\" class=\"edit\" /> &nbsp; &nbsp;";
  aParam += "</label> <br />";
  zeDiv.innerHTML += aParam;

  
}

function manageResult(text) {
	document.getElementById("resultSet").innerHTML = text;
}

function manageXml(text) {
	document.getElementById("bajaxml").innerHTML = text;
}

// Calcule la requete bajax xml
function displayQuery(className,method, params) {
  var query = createBalloonXmlQuery(className, method, params)
  query = escapeXMLContent(query) 
  query = query.replace(/ /gi,"&nbsp;");
  query = query.replace(/\n/gi,"<br />");
  query = "<code>"+query+"<code>";
  manageXml(query);

}

function manageError(text) {
  var t = "<center> <span class=\"error\">";
  t += text;
  t += "</span> </center>";
  manageXml(t);

}